package com.zl.anova
{
	import com.zl.bean.EBean;
	import com.zl.bean.FactorBean;
	import com.zl.bean.HBean;
	import com.zl.bean.HDgBean;
	import com.zl.bean.TBean;
	
	import common.CommonData;
	
	import flash.utils.Dictionary;
	
	import main.mainView;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.CheckBox;
	
	public class Anova
	{
		
		[Bindable] public var heDataList:ArrayCollection;
		
		[Bindable] public var heDataList1:ArrayCollection = new ArrayCollection(); 
		[Bindable] public var heDataList2:ArrayCollection = new ArrayCollection(); 
		[Bindable] public var heDataList3:ArrayCollection = new ArrayCollection(); 
		[Bindable] public var heDataList4:ArrayCollection = new ArrayCollection(); 
		[Bindable] public var heDataList5:ArrayCollection = new ArrayCollection(); 
		[Bindable] public var heDataList6:ArrayCollection = new ArrayCollection(); 
		[Bindable] public var heDataList7:ArrayCollection = new ArrayCollection(); 
		[Bindable] public var heDataList8:ArrayCollection = new ArrayCollection(); 
		
		[Bindable] public var hDgList:ArrayCollection = new ArrayCollection();
		[Bindable] public var dataList:ArrayCollection = new ArrayCollection();
		[Bindable] public var data1List:ArrayCollection = new ArrayCollection();
		[Bindable] public var hdList:ArrayCollection = new ArrayCollection(); 
		
		public var level:int = 0;
		
		
		//etc
		private var elementCount:int = 0;
		private var elementArray:Array = [];
		private var elementGroupValueArray:Array = [];
		private var elementGroupList:ArrayCollection = new ArrayCollection(); 
		
		private var resultStr:String = "";
		
		public function calculateAnova(heDataList:ArrayCollection):void{
			this.heDataList = heDataList;
			hdList = new ArrayCollection();
			CommonData.getInstance().step3HDgList.removeAll();
			elementCount = CommonData.getInstance().showNotPassList.length;
			elementArray = new Array();
			
			for(var i:int = 0; i < elementCount; i++){
				var hBean:HBean = new HBean();
				var elementBean:FactorBean = new FactorBean();
				elementBean = (CommonData.getInstance().showNotPassList.getItemAt(i) as HBean).fBean;
				elementArray.push(elementBean);
			}
			
			hDgList.removeAll();
			if(elementCount == 2 || elementCount == 3){
				this.level = 2;
				showStep3HDg(level);
				doAnovaLevel2();
			}
				///////////////////删掉4因素的
			else{
				Alert.show("影响因素个数不正确，无法进行水平分析！");
			}
			
			CommonData.getInstance().step3HDgList = hDgList;
		}
		
		private function doAnovaLevel2():void{
			//分组
			var notPassBean1:FactorBean = (CommonData.getInstance().showNotPassList.getItemAt(0) as HBean).fBean;
			var notPassBean2:FactorBean = (CommonData.getInstance().showNotPassList.getItemAt(1) as HBean).fBean;
			if(elementCount == 2){		//两因素二水平
				for each(var item:Object in heDataList){
					group2Level2Element(item[notPassBean1.id], item[notPassBean2.id], item[CommonData.getInstance().treeType]);
				}
				calculateresult2f(notPassBean1, notPassBean2, level);
			}else if(elementCount == 3){		//三因素二水平
				var notPassBean3:FactorBean = (CommonData.getInstance().showNotPassList.getItemAt(2) as HBean).fBean;
				for each(var item:Object in heDataList){
					//group2Level();
					group2Level3Element(item[notPassBean1.id], item[notPassBean2.id], item[notPassBean3.id], item[CommonData.getInstance().treeType]);
				}
				calculate3Element2Level(notPassBean1, notPassBean2, notPassBean3, level);
			}
		}
		
		////////////////删掉4因素3水平的程序
		
		//显示第三步中的水平范围
		private function showStep3HDg(level:int):void{
			elementGroupList.removeAll();
			for(var i:int=0; i < elementArray.length; i++){
				elementGroupValueArray = new Array();
				//求分组点，min,ha,max
				for(var j:int = 0; j <= level; j++){
					var groupValue:Number = 0;
					if(j == 0){		
						groupValue = elementArray[i].hMin;	//计算最小值  即：groupValueArray[0] = min
					}else if(j == level){	
						groupValue = elementArray[i].hMax;	//计算最大值 即：groupValueArray[1] = max
					}else{
						groupValue = elementGroupValueArray[j-1] + ((elementArray[i].hMax - elementArray[i].hMin)/level);
					}
					elementGroupValueArray.push(groupValue);
				}
				
				for(var m:int = 0; m < level; m++){
					var k:int = m+1;
					var hDg1:HDgBean = new HDgBean(elementArray[i].label + k + "水平", 
						elementGroupValueArray[m].toFixed(4), elementGroupValueArray[m+1].toFixed(4));
					hDgList.addItem(hDg1);
				}
				elementGroupList.addItem(elementGroupValueArray);
			}
		}
		
		private function group2Level2Element(mm:Number,mn:Number,v:String):void{
			var hamin:Number = (elementGroupList.getItemAt(0) as Array)[0];
			var ha1:Number = (elementGroupList.getItemAt(0) as Array)[1];
			var hamax:Number = (elementGroupList.getItemAt(0) as Array)[2];
			var hbmin:Number = (elementGroupList.getItemAt(1) as Array)[0];
			var hb1:Number = (elementGroupList.getItemAt(1) as Array)[1];
			var hbmax:Number = (elementGroupList.getItemAt(1) as Array)[2];
			if (mm>=hamin && mm<ha1 && mn>=hbmin && mn<hb1){
				heDataList1.addItem(v);
			}else if (mm>=hamin && mm<ha1 && mn>=hb1 && mn<=hbmax){
				heDataList2.addItem(v);
			}else if (mm>=ha1 && mm<=hamax && mn>=hbmin && mn<hb1){
				heDataList3.addItem(v);
			}else if (mm>=ha1 && mm<=hamax && mn>=hb1 && mn<=hbmax){
				heDataList4.addItem(v);
			}
		}
		
		private function group2Level3Element(mm:Number,mn:Number,mk:Number,v:String):void{
			var hamin:Number = (elementGroupList.getItemAt(0) as Array)[0];
			var ha1:Number = (elementGroupList.getItemAt(0) as Array)[1];
			var hamax:Number = (elementGroupList.getItemAt(0) as Array)[2];
			var hbmin:Number = (elementGroupList.getItemAt(1) as Array)[0];
			var hb1:Number = (elementGroupList.getItemAt(1) as Array)[1];
			var hbmax:Number = (elementGroupList.getItemAt(1) as Array)[2];
			var hcmin:Number = (elementGroupList.getItemAt(2) as Array)[0];
			var hc1:Number = (elementGroupList.getItemAt(2) as Array)[1];
			var hcmax:Number = (elementGroupList.getItemAt(2) as Array)[2];
			
			if (mm>=hamin&&mm<ha1&&mn>=hbmin&&mn<hb1&&mk>=hcmin&&mk<hc1){//m2m3m5为三因素，实验水平分别为111
				heDataList1.addItem(v);
			}else if (mm>=hamin&&mm<ha1&&mn>=hbmin&&mn<hb1&&mk>=hc1&&mk<=hcmax){    //112
				heDataList2.addItem(v);
			}else if (mm>=hamin&&mm<ha1&&mn>=hb1&&mn<=hbmax&&mk>=hcmin&&mk<hc1){    //121
				heDataList3.addItem(v);
			}else if (mm>=hamin&&mm<ha1&&mn>=hb1&&mn<=hbmax&&mk>=hc1&&mk<=hcmax){    //122
				heDataList4.addItem(v);
			}else if (mm>=ha1&&mm<=hamax&&mn>=hbmin&&mn<hb1&&mk>=hcmin&&mk<hc1){   //211
				heDataList5.addItem(v);
			}else if (mm>=ha1&&mm<=hamax&&mn>=hbmin&&mn<hb1&&mk>=hc1&&mk<=hcmax){    //212
				heDataList6.addItem(v);
			}else if (mm>=ha1&&mm<=hamax&&mn>=hb1&&mn<=hbmax&&mk>=hcmin&&mk<hc1){   //221 
				heDataList7.addItem(v);
			}else if (mm>=ha1&&mm<=hamax&&mn>=hb1&&mn<=hbmax&&mk>=hc1&&mk<=hcmax){//222
				heDataList8.addItem(v);
			}
		}
		
		///////////////删掉4因素3水平的分组
		
		private function analysis2f(Y1:Number,Y2:Number,Y3:Number,Y4:Number,s1:String, s2:String):void{
			var hamin:Number = (elementGroupList.getItemAt(0) as Array)[0];
			var ha1:Number = (elementGroupList.getItemAt(0) as Array)[1];
			var hamax:Number = (elementGroupList.getItemAt(0) as Array)[2];
			var hbmin:Number = (elementGroupList.getItemAt(1) as Array)[0];
			var hb1:Number = (elementGroupList.getItemAt(1) as Array)[1];
			var hbmax:Number = (elementGroupList.getItemAt(1) as Array)[2];
			var alertStr1:String = "数据不足，无法进行分析，请进行相应水平的实验并重新分析！";
			var alertStr2:String = "对应水平的数据范围为:";
			var alertStr3:String = ""; 
			var i:uint = 0;
			if(Y1==0){
				i++;
				alertStr3 += i+'、'+s1+':'+hamin.toFixed(4)+'-'+ha1.toFixed(4)+'\n';
				alertStr3 += '   '+s2+':'+hbmin.toFixed(4)+'-'+hb1.toFixed(4)+'\n';
			}
			if(Y2==0){
				i++;
				alertStr3 += i+'、'+s1+':'+hamin.toFixed(4)+'-'+ha1.toFixed(4)+'\n';
				alertStr3 += '   '+s2+':'+hb1.toFixed(4)+'-'+hbmax.toFixed(4)+'\n';
			}
			if(Y3==0){
				i++;
				alertStr3 += i+'、'+s1+':'+ha1.toFixed(4)+'-'+hamax.toFixed(4)+'\n';
				alertStr3 += '   '+s2+':'+hbmin.toFixed(4)+'-'+hb1.toFixed(4)+'\n';
			}
			if(Y4==0){
				i++;
				alertStr3 += i+'、'+s1+':'+ha1.toFixed(4)+'-'+hamax.toFixed(4)+'\n';
				alertStr3 += '   '+s2+':'+hb1.toFixed(4)+'-'+hbmax.toFixed(4)+'\n';
			}
			Alert.show(alertStr1+'\n'+alertStr2+'\n'+alertStr3);
			CommonData.getInstance().step3Result = "<font color='#0000ff' size='12'>各因数影响结果比较：</font>";
			CommonData.getInstance().step3Pro =  "<font color='#0000ff' size='12'>最优组合：</font>";
		}
		
		private function showresult2f(a:Number, b:Number, d:Number, ave1:Number, ave2:Number,ave3:Number,
									  ave4:Number, s1:String,  s2:String, s4:String, s5:String, s6:String,s7:String):void{
			var hamin:Number = (elementGroupList.getItemAt(0) as Array)[0];
			var ha1:Number = (elementGroupList.getItemAt(0) as Array)[1];
			var hamax:Number = (elementGroupList.getItemAt(0) as Array)[2];
			var hbmin:Number = (elementGroupList.getItemAt(1) as Array)[0];
			var hb1:Number = (elementGroupList.getItemAt(1) as Array)[1];
			var hbmax:Number = (elementGroupList.getItemAt(1) as Array)[2];
			CommonData.getInstance().step3Result = "";
			CommonData.getInstance().step3Pro = "";
			resultStr = "<font color='#0000ff' size='12'>各因数影响结果比较：</font>";
			var proStr:String = "<font color='#0000ff' size='12'>最优组合：</font>";
			if (a>=d){
				if (b>=d){
					if (a>=b){
						resultStr = "<font color='#0000ff' size='12'>各因数影响结果比较：</font>\n\n影响程度为："+s1+" > "+s2;
					}
					else if (a<b){
						resultStr = "<font color='#0000ff' size='12'>各因数影响结果比较：</font>\n\n影响程度为："+s2+" > "+s1;
					}
				}else if (b<d){
					resultStr = "<font color='#0000ff' size='12'>各因数影响结果比较：</font>\n\n影响程度为："+s1+" > "+s2;
				}
			}else if (a<d){
				if (b>=d){
					resultStr = "<font color='#0000ff' size='12'>各因数影响结果比较：</font>\n\n影响程度为："+s2+" > "+s1;
				}else if (b<d){
					if (a>=b){			
						resultStr = "<font color='#0000ff' size='12'>各因数影响结果比较：</font>\n\n影响程度为："+s1+" > "+s2;
					}else if (a<b){
						resultStr = "<font color='#0000ff' size='12'>各因数影响结果比较：</font>\n\n影响程度为："+s2+" > "+s1;
					}
				}
			}
			CommonData.getInstance().step3Result = resultStr;
			if (ave1>ave2&&ave3>ave4){
				proStr = "<font color='#0000ff' size='12'>最优组合：</font>\n\n" + s4 + "、"+s6  +";\n\n" + 
					"对应水平的数据范围为：\n"+s1+":"+hamin.toFixed(4)+"-"+ha1.toFixed(4)+"\n"+s2+":"+hbmin.toFixed(4)+"-"+hb1.toFixed(4);
			}else if (ave1>ave2&&ave3<=ave4){
				proStr = "<font color='#0000ff' size='12'>最优组合：</font>\n\n" + s4 + "、"+s7  +";\n\n" + 
					"对应水平的数据范围为：\n"+s1+":"+hamin.toFixed(4)+"-"+ha1.toFixed(4)+"\n"+s2+":"+hb1.toFixed(4)+"-"+hbmax.toFixed(4);
			}else if (ave1<=ave2&&ave3>ave4){
				proStr = "<font color='#0000ff' size='12'>最优组合：</font>\n\n" + s5 + "、"+s6  +";\n\n" + 
					"对应水平的数据范围为：\n"+s1+":"+ha1.toFixed(4)+"-"+hamax.toFixed(4)+"\n"+s2+":"+hbmin.toFixed(4)+"-"+hb1.toFixed(4);
			}else if (ave1<=ave2&&ave3<=ave4){
				proStr = "<font color='#0000ff' size='12'>最优组合：</font>\n\n" + s5 + "、"+s7  +";\n\n" + 
					"对应水平的数据范围为：\n"+s1+":"+ha1.toFixed(4)+"-"+hamax.toFixed(4)+"\n"+s2+":"+hb1.toFixed(4)+"-"+hbmax.toFixed(4);
			}
			CommonData.getInstance().step3Pro = proStr;
		}
		
		private function calculateresult2f(notPassBean1:FactorBean, notPassBean2:FactorBean, level:int):void{
			CommonData.getInstance().step3SDgList.removeAll();
			var sumY1:Number = calculateysum(heDataList1);
			var sumY2:Number = calculateysum(heDataList2);
			var sumY3:Number = calculateysum(heDataList3);
			var sumY4:Number = calculateysum(heDataList4);
			var sumT:Number = sumY1 + sumY2 + sumY3 + sumY4 ;
			
			var testItemList:ArrayCollection = new ArrayCollection();//检测项list
			testItemList.addAll(heDataList1);
			testItemList.addAll(heDataList2);
			testItemList.addAll(heDataList3);
			testItemList.addAll(heDataList4);
			
			
			var squaresum:Number = calculatesquaresum(testItemList);//elementGroupList是什么list，是总的list吗
			
			var numberA1B1:Number = heDataList1.length ;
			var numberA1B2:Number = heDataList2.length ;
			var numberA2B1:Number = heDataList3.length ;
			var numberA2B2:Number = heDataList4.length ;
			var numberA1:Number = numberA1B1 + numberA1B2 ;
			var numberA2:Number = numberA2B1 + numberA2B2 ;
			var numberB1:Number = numberA1B1 + numberA2B1 ;
			var numberB2:Number = numberA1B2 + numberA2B2 ;
			var numberT:Number = numberA1 + numberA2 ;
			
			
			if(sumY1==0 || sumY2 ==0 || sumY3 == 0 || sumY4 ==0){
				analysis2f(sumY1,sumY2,sumY3,sumY4,notPassBean1.label, notPassBean2.label);
			}else{
				
				var SA:Number = ((sumY1 + sumY2)*(sumY1 + sumY2)) /numberA1 + 
					((sumY3 + sumY4)*(sumY3 + sumY4)) /numberA2 - (sumT*sumT) /numberT ;
				var SB:Number = ((sumY1 + sumY3)*(sumY1 + sumY3)) /numberB1 + 
					((sumY2 + sumY4)*(sumY2 + sumY4)) /numberB2 - (sumT*sumT) /numberT ;
				/*var SAB:Number = (sumY1*sumY1) / numberA1B1 + (sumY2*sumY2) / numberA1B2 +
					(sumY3*sumY3) / numberA2B1 + (sumY4*sumY4) / numberA2B2 - SA - SB - 
					(sumT*sumT) /numberT ;*/
				
				var ST:Number = squaresum - (sumT*sumT) /numberT ;
				var Se:Number = ST - SA - SB ;
				
				var level:int = 2;
				var F:Number = 0;
				var fA:Number = (level-1);
				var fB:Number = (level-1);
				var fAB:Number = fA*fB ;
				var fT:Number = (numberT-1);//水平数*实验重复次数
				var fe:Number = fT-fA-fB-fAB;
				
				var MSA:Number = SA/fA;
				var MSB:Number = SB/fB;
			//	var MSAB:Number = SAB/fAB;
				var MSe:Number = Se/fe;
				
				var FA:Number = MSA/MSe;
				var FB:Number = MSB/MSe;
			//	var FAB:Number = MSAB/MSe;
				
				if(level == 2){
					if ((fe)>120)
					{
						F=3.84;//需比较的F值F0.95(1,无穷)
					}
					if (fe>60&&fe<=120)
					{
						F=3.92;//需比较的F值F0.95(1,120)
					}
					if (fe>40&&fe<=60)
					{
						F=4.0;//需比较的F值F0.95(1,60)
					}
					if (fe>30&&fe<=40)
					{
						F=4.08;//需比较的F值F0.95(1,40)
					}
					if (fe==30)
					{
						F=4.17;}
					if (fe==29)
					{
						F=4.18;}
					if (fe==28)
					{
						F=4.20;}
					if (fe==27)
					{
						F=4.21;}
					if (fe==26)
					{
						F=4.23;}
					if (fe<=25&&fe>20)
					{
						F=4.32-(fe-21)*0.02;//需比较的F值F0.95(1,40)
					}
					if (fe>17&&fe<=20)
					{
						F=4.41-(fe-18)*0.03;}
					if (fe==17)
					{
						F=4.45;}
					if (fe==16)
					{
						F=4.49;}
					if (fe==15)
					{
						F=4.54;}
					if (fe==14)
					{
						F=4.60;}
					if (fe==13)
					{
						F=4.67;}
					if (fe==12)
					{
						F=4.75;}
					if (fe==11)
					{
						F=4.84;}
					if (fe==10)
					{
						F=4.96;}
					if (fe==9)
					{
						F=5.12;}
					if (fe==8)
					{
						F=5.32;}
					if (fe==7)
					{
						F=5.59;}
				}
				
				var hBean:HBean;
				var eBean:EBean = new EBean();
				var tBean:TBean = new TBean();
				
				hBean = new HBean();
				hBean.title = notPassBean1.label;
				hBean.sNum = SA.toFixed(4);
				hBean.fNum = fA.toString();
				hBean.msNum = MSA.toFixed(4);
				hBean.FANum = FA.toFixed(4);
				data1List.addItem(hBean);
				
				hBean = new HBean();
				hBean.title = notPassBean2.label;
				hBean.sNum = SB.toFixed(4);
				hBean.fNum = fB.toString();
				hBean.msNum = MSB.toFixed(4);
				hBean.FANum = FB.toFixed(4);
				data1List.addItem(hBean);
				
			/*	hBean = new HBean();
				hBean.title = "AB";
				hBean.sNum = SAB.toFixed(4);
				hBean.fNum = fAB.toString();
				hBean.msNum = MSAB.toFixed(4);
				hBean.FANum = FAB.toFixed(4);
				data1List.addItem(hBean);
			*/	
				eBean.title = "e";
				eBean.sNum = Se.toFixed(4);
				eBean.fNum = fe.toString();
				eBean.msNum = MSe.toFixed(4);
				eBean.FANum = "-";
				
				tBean.title = "T";
				tBean.sNum = ST.toFixed(4);
				tBean.fNum = fT.toString();
				tBean.msNum = "-";
				tBean.FANum = F.toFixed(4);
				data1List.addItem(eBean);
				data1List.addItem(tBean);
				CommonData.getInstance().step3SDgList = data1List;
				
				
				showresult2f(FA,FB,F,sumY1,sumY2,sumY3,sumY4,notPassBean1.label, notPassBean2.label,
					notPassBean1.label + "1水平", notPassBean1.label + "2水平", notPassBean2.label + "1水平",notPassBean2.label + "2水平");
			}
			/*if(Y1==0 || Y2 ==0 || Y3 == 0 || Y4 ==0){
			analysis2f(Y1,Y2,Y3,Y4,notPassBean1.label, notPassBean2.label);
			}else{
			showresult2f(FA,FB,F,aveAT1,aveAT2,aveBT1,aveBT2,notPassBean1.label, notPassBean2.label,
			notPassBean1.label + "1水平", notPassBean1.label + "2水平", notPassBean2.label + "1水平",notPassBean2.label + "2水平");
			}*/
		}
		///////////3因素2水平
		private function calculate3Element2Level(notPassBean1:FactorBean, notPassBean2:FactorBean,notPassBean3:FactorBean, level:int):void{
			CommonData.getInstance().step3SDgList.removeAll();
			var sumY1:Number = calculateysum(heDataList1);
			var sumY2:Number = calculateysum(heDataList2);
			var sumY3:Number = calculateysum(heDataList3);
			var sumY4:Number = calculateysum(heDataList4);
			var sumY5:Number = calculateysum(heDataList5);
			var sumY6:Number = calculateysum(heDataList6);
			var sumY7:Number = calculateysum(heDataList7);
			var sumY8:Number = calculateysum(heDataList8);
			var sumT:Number = sumY1 + sumY2 + sumY3 + sumY4 + sumY5 + sumY6 + sumY7 + sumY8;
			
			var testItemList:ArrayCollection = new ArrayCollection();//检测项list
			testItemList.addAll(heDataList1);
			testItemList.addAll(heDataList2);
			testItemList.addAll(heDataList3);
			testItemList.addAll(heDataList4);
			testItemList.addAll(heDataList5);
			testItemList.addAll(heDataList6);
			testItemList.addAll(heDataList7);
			testItemList.addAll(heDataList8);
			
			var squaresum:Number = calculatesquaresum(testItemList);
			
			var numberA1B1C1:Number = heDataList1.length ;
			var numberA1B1C2:Number = heDataList2.length ;
			var numberA1B2C1:Number = heDataList3.length ;
			var numberA1B2C2:Number = heDataList4.length ;
			var numberA2B1C1:Number = heDataList5.length ;
			var numberA2B1C2:Number = heDataList6.length ;
			var numberA2B2C1:Number = heDataList7.length ;
			var numberA2B2C2:Number = heDataList8.length ;
			var numberA1:Number = numberA1B1C1 + numberA1B1C2 + numberA1B2C1 + numberA1B2C2 ;
			var numberA2:Number = numberA2B1C1 + numberA2B1C2 + numberA2B2C1 + numberA2B2C2 ;
			var numberB1:Number = numberA1B1C1 + numberA1B1C2 + numberA2B1C1 + numberA2B1C2 ;
			var numberB2:Number = numberA1B2C1 + numberA1B2C2 + numberA2B2C1 + numberA2B2C2 ;
			var numberC1:Number = numberA1B1C1 + numberA1B2C1 + numberA2B1C1 + numberA2B2C1 ;
			var numberC2:Number = numberA1B1C2 + numberA1B2C2 + numberA2B1C2 + numberA2B2C2 ;
			var numberT:Number = numberA1 + numberA2 ;
			
			if(sumY1==0 || sumY2 ==0 || sumY3 == 0 || sumY4 ==0 || sumY5==0 || sumY6 ==0 || sumY7 == 0 || sumY8 ==0){
				analysis3f(sumY1,sumY2,sumY3,sumY4,sumY5,sumY6,sumY7,sumY8,notPassBean1.label, notPassBean2.label, notPassBean3.label);
			}else{
				var SA:Number = ((sumY1 + sumY2 + sumY3 + sumY4)*(sumY1 + sumY2 + sumY3 + sumY4)) /numberA1 + 
					((sumY5 + sumY6 + sumY7 + sumY8)*(sumY5 + sumY6 + sumY7 + sumY8)) /numberA2 - (sumT*sumT) /numberT ;
				var SB:Number = ((sumY1 + sumY2 + sumY5 + sumY6)*(sumY1 + sumY2 + sumY5 + sumY6)) /numberB1 + 
					((sumY3 + sumY4 + sumY7 + sumY8)*(sumY3 + sumY4 + sumY7 + sumY8)) /numberB2 - (sumT*sumT) /numberT ;
				var SC:Number = ((sumY1 + sumY3 + sumY5 + sumY7)*(sumY1 + sumY3 + sumY5 + sumY7)) /numberC1 + 
					((sumY2 + sumY4 + sumY6 + sumY8)*(sumY2 + sumY4 + sumY6 + sumY8)) /numberC2 - (sumT*sumT) /numberT ;
				/*var SAB:Number = ((sumY1 + sumY2)*(sumY1 + sumY2)) / (numberA1B1C1 + numberA1B1C2) + 
					((sumY3 + sumY4)*(sumY3 + sumY4)) / (numberA1B2C1 + numberA1B2C2) +
					((sumY5 + sumY6)*(sumY5 + sumY6)) / (numberA2B1C1 + numberA2B1C2) +
					((sumY7 + sumY8)*(sumY7 + sumY8)) / (numberA2B2C1 + numberA2B2C2) - 
					SA - SB - (sumT*sumT) /numberT ;
				var SAC:Number = ((sumY1 + sumY3)*(sumY1 + sumY3)) / (numberA1B1C1 + numberA1B2C1) + 
					((sumY2 + sumY4)*(sumY2 + sumY4)) / (numberA1B2C2 + numberA1B1C2) +
					((sumY5 + sumY7)*(sumY5 + sumY7)) / (numberA2B1C1 + numberA2B2C1) +
					((sumY6 + sumY8)*(sumY6 + sumY8)) / (numberA2B2C2 + numberA2B1C2) - 
					SA - SC - (sumT*sumT) /numberT ;
				var SBC:Number = ((sumY1 + sumY5)*(sumY1 + sumY5)) / (numberA1B1C1 + numberA2B1C1) + 
					((sumY2 + sumY6)*(sumY2 + sumY6)) / (numberA1B1C2 + numberA2B1C2) +
					((sumY3 + sumY7)*(sumY3 + sumY7)) / (numberA2B2C1 + numberA1B2C1) +
					((sumY4 + sumY8)*(sumY4 + sumY8)) / (numberA1B2C2 + numberA2B2C2) - 
					SB - SC - (sumT*sumT) /numberT ;
				var SABC:Number = (sumY1*sumY1)/numberA1B1C1 + (sumY2*sumY2)/numberA1B1C2 + (sumY3*sumY3)/numberA1B2C1
					+ (sumY4*sumY4)/numberA1B2C2 + (sumY5*sumY5)/numberA2B1C1 + (sumY6*sumY6)/numberA2B1C2 
					+ (sumY7*sumY7)/numberA2B2C1 + (sumY8*sumY8)/numberA2B2C2 - SA - SB - SC - SAB - SAC - SBC
					- 4*(sumT*sumT) /numberT ;*/
				/*var Se:Number = squaresum - ((sumY1*sumY1)/numberA1B1C1 + (sumY2*sumY2)/numberA1B1C2 + 
					(sumY3*sumY3)/numberA1B2C1 + (sumY4*sumY4)/numberA1B2C2 + (sumY5*sumY5)/numberA2B1C1 
					+ (sumY6*sumY6)/numberA2B1C2 + (sumY7*sumY7)/numberA2B2C1 + (sumY8*sumY8)/numberA2B2C2) ;*/
				
				var ST:Number = squaresum - (sumT*sumT) /numberT ;
				var Se:Number = ST-SA-SB-SC;
				
				var aveAArry:Array =[];
				var aveAT1:Number = sumY1 + sumY2 + sumY3 + sumY4;
				var aveAT2:Number = sumY5 + sumY6 + sumY7 + sumY8;
				aveAArry.push(aveAT1);	
				aveAArry.push(aveAT2);	
				var indexA:int = calculateIndex(aveAArry);
				
				var aveBArry:Array =[];
				var aveBT1:Number = sumY1 + sumY2 + sumY5 + sumY6;
				var aveBT2:Number = sumY3 + sumY4 + sumY7 + sumY8;
				aveBArry.push(aveBT1);	
				aveBArry.push(aveBT2);	
				var indexB:int = calculateIndex(aveBArry);
				
				var aveCArry:Array =[];
				var aveCT1:Number = sumY1 + sumY3 + sumY5 + sumY7;
				var aveCT2:Number = sumY2 + sumY4 + sumY6 + sumY8;
				aveCArry.push(aveCT1);	
				aveCArry.push(aveCT2);	
				var indexC:int = calculateIndex(aveCArry);
				
				var level:int = 2;
				var F:Number = 0;
				var fA:Number = (level-1);
				var fB:Number = (level-1);
				var fC:Number = (level-1);
				var fAB:Number = fA*fB;
				var fBC:Number = fB*fC;
				var fAC:Number = fA*fC;
				var fABC:Number = fA*fB*fC;
				var fT:Number = numberT-1;//水平数*实验重复次数
				var fe:Number = numberT-3*level;
				
				var MSA:Number = SA/fA;
				var MSB:Number = SB/fB;
				var MSC:Number = SC/fC;
				/*var MSAB:Number = SAB/fAB;
				var MSBC:Number = SBC/fBC;
				var MSAC:Number = SAC/fAC;
				var MSABC:Number = SABC/fABC;*/
				var MSe:Number = Se/fe;
				
				var FA:Number = MSA/MSe;
				var FB:Number = MSB/MSe;
				var FC:Number = MSC/MSe;
			/*	var FAB:Number = MSAB/MSe;
				var FBC:Number = MSBC/MSe;
				var FAC:Number = MSAC/MSe;
				var FABC:Number = MSABC/MSe;*/
				
				if(level == 2){
					if ((fe)>120)
					{
						F=3.84;//需比较的F值F0.95(1,无穷)
					}
					if (fe>60&&fe<=120)
					{
						F=3.92;//需比较的F值F0.95(1,120)
					}
					if (fe>40&&fe<=60)
					{
						F=4.0;//需比较的F值F0.95(1,60)
					}
					if (fe>30&&fe<=40)
					{
						F=4.08;//需比较的F值F0.95(1,40)
					}
					if (fe==30)
					{
						F=4.17;}
					if (fe==29)
					{
						F=4.18;}
					if (fe==28)
					{
						F=4.20;}
					if (fe==27)
					{
						F=4.21;}
					if (fe==26)
					{
						F=4.23;}
					if (fe<=25&&fe>20)
					{
						F=4.32-(fe-21)*0.02;//需比较的F值F0.95(1,40)
					}
					if (fe>17&&fe<=20)
					{
						F=4.41-(fe-18)*0.03;}
					if (fe==17)
					{
						F=4.45;}
					if (fe==16)
					{
						F=4.49;}
					if (fe==15)
					{
						F=4.54;}
					if (fe==14)
					{
						F=4.60;}
					if (fe==13)
					{
						F=4.67;}
					if (fe==12)
					{
						F=4.75;}
					if (fe==11)
					{
						F=4.84;}
					if (fe==10)
					{
						F=4.96;}
					if (fe==9)
					{
						F=5.12;}
					if (fe==8)
					{
						F=5.32;}
					if (fe==7)
					{
						F=5.59;}
				}
				
				var hBean:HBean;
				var eBean:EBean = new EBean();
				var tBean:TBean = new TBean();
				
				hBean = new HBean();
				hBean.title = notPassBean1.label;
				hBean.sNum = SA.toFixed(4);
				hBean.fNum = fA.toString();
				hBean.msNum = MSA.toFixed(4);
				hBean.FANum = FA.toFixed(4);
				data1List.addItem(hBean);
				
				hBean = new HBean();
				hBean.title = notPassBean2.label;
				hBean.sNum = SB.toFixed(4);
				hBean.fNum = fB.toString();
				hBean.msNum = MSB.toFixed(4);
				hBean.FANum = FB.toFixed(4);
				data1List.addItem(hBean);
				
				hBean = new HBean();
				hBean.title = notPassBean3.label;
				hBean.sNum = SC.toFixed(4);
				hBean.fNum = fC.toString();
				hBean.msNum = MSC.toFixed(4);
				hBean.FANum = FC.toFixed(4);
				data1List.addItem(hBean);
				
				eBean.title = "e";
				eBean.sNum = Se.toFixed(4);
				eBean.fNum = fe.toString();
				eBean.msNum = MSe.toFixed(4);
				eBean.FANum = "-";
				
				tBean.title = "T";
				tBean.sNum = ST.toFixed(4);
				tBean.fNum = fT.toString();
				tBean.msNum = "-";
				tBean.FANum = F.toFixed(4);
				data1List.addItem(eBean);
				data1List.addItem(tBean);
				CommonData.getInstance().step3SDgList = data1List;
				
				showresult3E2L(FA,FB,FC,F,indexA,indexB,indexC);
			}
		}
		
		private function showresult3E2L(a:Number, b:Number, c:Number,f:Number, indexA:Number, indexB:Number,indexC:Number):void{
			CommonData.getInstance().step3Result = "";
			CommonData.getInstance().step3Pro = "";
			var resultStr:String = "<font color='#0000ff' size='12'>各因数影响结果比较：</font>";
			
			var eArray:Array = [];
			var tempArray:Array = [];
			var resultArray:Array = [];
			
			eArray.push({productName:elementArray[0].label,version:a});
			eArray.push({productName:elementArray[1].label,version:b});
			eArray.push({productName:elementArray[2].label,version:c});
			eArray.sortOn("version",Array.NUMERIC);
			for(var i:int=0; i<eArray.length; i++){
				resultArray.push(eArray[i].productName);
			}
			resultStr = "<font color='#0000ff' size='12'>各因数影响结果比较：</font>\n\n影响程度为："+resultArray[2]+" > "+resultArray[1]+" > "+resultArray[0];
			CommonData.getInstance().step3Result = resultStr;
			
			var proStr:String = "<font color='#0000ff' size='12'>最优组合：</font>";
			var ma:int = indexA + 1;
			var labelA:String = elementArray[0].label + ma + "水平";
			var mb:int = indexB + 1;
			var labelB:String = elementArray[1].label + mb + "水平";
			var mc:int = indexC + 1;
			var labelC:String = elementArray[2].label + mc + "水平";
			proStr = "<font color='#0000ff' size='12'>最优组合：</font>\n\n" + labelA + "、"+ labelB  + "、" + labelC+ ";\n\n"
				+ "对应水平的数据范围为：\n"+elementArray[0].label+":"+(elementGroupList.getItemAt(0) as Array)[indexA].toFixed(4)+"-"+(elementGroupList.getItemAt(0) as Array)[indexA+1].toFixed(4)
				+"\n"+elementArray[1].label+":"+(elementGroupList.getItemAt(1) as Array)[indexB].toFixed(4)+"-"+(elementGroupList.getItemAt(1) as Array)[indexB+1].toFixed(4)
				+"\n"+elementArray[2].label+":"+(elementGroupList.getItemAt(2) as Array)[indexC].toFixed(4)+"-"+(elementGroupList.getItemAt(2) as Array)[indexC+1].toFixed(4);
			CommonData.getInstance().step3Pro = proStr;
		}
		
		private function analysis3f(Y1:Number,Y2:Number,Y3:Number,Y4:Number,Y5:Number,Y6:Number,
									Y7:Number,Y8:Number,s1:String, s2:String, s3:String):void{
			var hamin:Number = (elementGroupList.getItemAt(0) as Array)[0];
			var ha1:Number = (elementGroupList.getItemAt(0) as Array)[1];
			var hamax:Number = (elementGroupList.getItemAt(0) as Array)[2];
			var hbmin:Number = (elementGroupList.getItemAt(1) as Array)[0];
			var hb1:Number = (elementGroupList.getItemAt(1) as Array)[1];
			var hbmax:Number = (elementGroupList.getItemAt(1) as Array)[2];
			var hcmin:Number = (elementGroupList.getItemAt(2) as Array)[0];
			var hc1:Number = (elementGroupList.getItemAt(2) as Array)[1];
			var hcmax:Number = (elementGroupList.getItemAt(2) as Array)[2];
			
			var alertStr1:String = "数据不足，无法进行分析，请进行相应水平的实验并重新分析！";
			var alertStr2:String = "对应水平的数据范围为:";
			var alertStr3:String = ""; 
			var i:uint = 0;
			if(Y1==0){
				i++;
				alertStr3 += i+'、'+s1+':'+hamin.toFixed(4)+'-'+ha1.toFixed(4)+'\n';
				alertStr3 += '   '+s2+':'+hbmin.toFixed(4)+'-'+hb1.toFixed(4)+'\n';
				alertStr3 += '   '+s3+':'+hcmin.toFixed(4)+'-'+hc1.toFixed(4)+'\n';
			}
			if(Y2==0){
				i++;
				alertStr3 += i+'、'+s1+':'+hamin.toFixed(4)+'-'+ha1.toFixed(4)+'\n';
				alertStr3 += '   '+s2+':'+hbmin.toFixed(4)+'-'+hb1.toFixed(4)+'\n';
				alertStr3 += '   '+s3+':'+hc1.toFixed(4)+'-'+hcmax.toFixed(4)+'\n';
			}
			if(Y3==0){
				i++;
				alertStr3 += i+'、'+s1+':'+hamin.toFixed(4)+'-'+ha1.toFixed(4)+'\n';
				alertStr3 += '   '+s2+':'+hb1.toFixed(4)+'-'+hbmax.toFixed(4)+'\n';
				alertStr3 += '   '+s3+':'+hcmin.toFixed(4)+'-'+hc1.toFixed(4)+'\n';
			}
			if(Y4==0){
				i++;
				alertStr3 += i+'、'+s1+':'+hamin.toFixed(4)+'-'+ha1.toFixed(4)+'\n';
				alertStr3 += '   '+s2+':'+hb1.toFixed(4)+'-'+hbmax.toFixed(4)+'\n';
				alertStr3 += '   '+s3+':'+hc1.toFixed(4)+'-'+hcmax.toFixed(4)+'\n';
			}
			if(Y5 == 0){
				i++;
				alertStr3 += i+'、'+s1+':'+ha1.toFixed(4)+'-'+hamax.toFixed(4)+'\n';
				alertStr3 += '   '+s2+':'+hbmin.toFixed(4)+'-'+hb1.toFixed(4)+'\n';
				alertStr3 += '   '+s3+':'+hcmin.toFixed(4)+'-'+hc1.toFixed(4)+'\n';
			}
			if(Y6 == 0){
				i++;
				alertStr3 += i+'、'+s1+':'+ha1.toFixed(4)+'-'+hamax.toFixed(4)+'\n';
				alertStr3 += '   '+s2+':'+hbmin.toFixed(4)+'-'+hb1.toFixed(4)+'\n';
				alertStr3 += '   '+s3+':'+hc1.toFixed(4)+'-'+hcmax.toFixed(4)+'\n';
			}
			if(Y7 == 0){
				i++;
				alertStr3 += i+'、'+s1+':'+ha1.toFixed(4)+'-'+hamax.toFixed(4)+'\n';
				alertStr3 += '   '+s2+':'+hb1.toFixed(4)+'-'+hbmax.toFixed(4)+'\n';
				alertStr3 += '   '+s3+':'+hcmin.toFixed(4)+'-'+hc1.toFixed(4)+'\n';
				
			}
			if(Y8 == 0){
				alertStr3 += i+'、'+s1+':'+ha1.toFixed(4)+'-'+hamax.toFixed(4)+'\n';
				alertStr3 += '   '+s2+':'+hb1.toFixed(4)+'-'+hbmax.toFixed(4)+'\n';
				alertStr3 += '   '+s3+':'+hc1.toFixed(4)+'-'+hcmax.toFixed(4)+'\n';
			}
			
			Alert.show(alertStr1+'\n'+alertStr2+'\n'+alertStr3);
			CommonData.getInstance().step3Result = "<font color='#0000ff' size='12'>各因数影响结果比较：</font>";
			CommonData.getInstance().step3Pro =  "<font color='#0000ff' size='12'>最优组合：</font>";
		}
		
		
		
		private function calculateysum(list:ArrayCollection):Number{
			var temp:Number = 0;
			var sum:Number = 0;
			if(list.length == 0){
				temp = 0;
			}else{
				for each(var item:Number in list){
					if(item){
						sum += item;
					}
				}
			}
			return sum;
		}
		
		private function calculatesquaresum(list:ArrayCollection):Number{
			var temp:Number = 0;
			var squaresum:Number = 0;
			if(list.length == 0){
				temp = 0;
			}else{
				for each(var item:Number in list){
					if(item){
						squaresum += item*item;
					}
				}
			}
			return squaresum;
		}
		
		private function calculateIndex(aveArray:Array):int{
			var index:int = 0;
			var max:Number = aveArray[0];
			var current:Number = 0;
			for(var i:int = 0; i < aveArray.length ;i++){
				current = aveArray[i];
				if(current >= max){
					max = current;
					index = i;
				}
			}
			return index;
		}
		
		private function calculateMaxIndex(aveArray:Array):Array{
			var max:Number = aveArray[0];
			var tempArray:Array = [];
			for(var i:int = 1; i < aveArray.length ;i++){
				if(aveArray[i] >= max){
					tempArray.push(i);
				}
			}
			return tempArray;
		}
	}
}